package com.dgut.epidemic.controller;

import com.dgut.epidemic.pojo.Daily_data;
import com.dgut.epidemic.pojo.abnormalData;
import com.dgut.epidemic.service.AdminDataService;
import com.dgut.epidemic.service.UserService;
import com.dgut.epidemic.utils.SubList;
import com.dgut.epidemic.utils.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
public class AdminDataController {

    int pageSize = 4;
    ExcelUtil excelUtil = new ExcelUtil();

    @Autowired
    private AdminDataService adminDataService;
    @Autowired
    private UserService userService;


    public AdminDataController() throws IOException {
    }

    @GetMapping("/admin/data")
    public String datalist(Model model) {
        PageInfo pageInfo = adminDataService.selectData(1,pageSize);
        List<Daily_data> datalist = pageInfo.getList();
        model.addAttribute("datalist",datalist);
        model.addAttribute("pageNum", 1);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("sort", "-1");
        model.addAttribute("starTime", "-1");
        model.addAttribute("endTime","-1");
        model.addAttribute("body","-1");
        model.addAttribute("istreatment","-1");
        model.addAttribute("isolated","-1");
        model.addAttribute("inschool","-1");
        model.addAttribute("inguan","-1");
        model.addAttribute("keyword","-1");
        model.addAttribute("roles",userService.getUserNow().getRoles());
        return "data_list";
    }

    @RequestMapping("/admin/listdata")
    public  String datList(Model model,String sort,String pageNum,String starTime,String endTime,
                           String body,String istreatment,String isolated,String inschool,String inguan,String keyword){
        PageInfo pageInfo = adminDataService.sortData(Integer.parseInt(pageNum),pageSize,sort,starTime,endTime,body,istreatment,isolated,inschool,inguan,keyword);
        List<Daily_data> datalist = pageInfo.getList();
        model.addAttribute("datalist",datalist);
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("totalPages", pageInfo.getPages());
        model.addAttribute("sort", sort);
        model.addAttribute("starTime", starTime);
        model.addAttribute("endTime",endTime);
        model.addAttribute("body",body);
        model.addAttribute("istreatment",istreatment);
        model.addAttribute("isolated",isolated);
        model.addAttribute("inschool",inschool);
        model.addAttribute("inguan",inguan);
        model.addAttribute("keyword",keyword);
        model.addAttribute("roles",userService.getUserNow().getRoles());
        return "data_list";
    }

    @RequestMapping("/admin/riskdata")
    public  String riskData(Model model) throws Exception {
        SubList subList = new SubList();
        List<Daily_data> daily_data = adminDataService.riskAreaData("","");
        List<Daily_data> datalist1 = subList.subList(daily_data,1,pageSize);
        List<Daily_data> datalist2 = subList.subList(adminDataService.selectTemperature("",""),1,pageSize);
        System.out.println(daily_data);
        System.out.println(adminDataService.selectTemperature("",""));
        PageInfo pageInfo = adminDataService.sortData(1,pageSize,"","","","!=","","","","","");
        model.addAttribute("datalist1",datalist1);
        model.addAttribute("datalist2",datalist2);
        model.addAttribute("datalist3",pageInfo.getList());
        model.addAttribute("totalPages1", (int)Math.ceil((double)daily_data.size()/pageSize));
        model.addAttribute("totalPages2", (int)Math.ceil((double)adminDataService.selectTemperature("","").size()/pageSize));
        model.addAttribute("totalPages3", pageInfo.getPages());
        model.addAttribute("pageNum1", 1);
        model.addAttribute("pageNum2", 1);
        model.addAttribute("pageNum3", 1);
        model.addAttribute("starTime", "-1");
        model.addAttribute("endTime","-1");
        model.addAttribute("heightRiskArea",adminDataService.heightRiskArea().toString().replace("[","").replace("]",""));
        model.addAttribute("middleRiskArea",adminDataService.middleRiskArea().toString().replace("[","").replace("]",""));
        model.addAttribute("roles",userService.getUserNow().getRoles());
        model.addAttribute("num",adminDataService.selectAbnormal());
        return "risk_data";
    }

    @RequestMapping("/admin/riskData")
    public  String RiskData(Model model,String pageNum1,String pageNum2,String pageNum3,String starTime,String endTime) throws Exception {
        SubList subList = new SubList();
        List<Daily_data> daily_data = adminDataService.riskAreaData(starTime,endTime);
        List<Daily_data> datalist1 = subList.subList(daily_data,Integer.parseInt(pageNum1),pageSize);
        List<Daily_data> datalist2 = subList.subList(adminDataService.selectTemperature(starTime,endTime),Integer.parseInt(pageNum2),pageSize);
        PageInfo pageInfo = adminDataService.sortData(Integer.parseInt(pageNum3),pageSize,"",starTime,endTime,"!=","","","","","");
        model.addAttribute("datalist1",datalist1);
        model.addAttribute("datalist2",datalist2);
        model.addAttribute("datalist3",pageInfo.getList());
        model.addAttribute("totalPages1", (int)Math.ceil((double)daily_data.size()/pageSize));
        model.addAttribute("totalPages2", (int)Math.ceil((double)adminDataService.selectTemperature(starTime,endTime).size()/pageSize));
        model.addAttribute("totalPages3", pageInfo.getPages());
        model.addAttribute("pageNum1", pageNum1);
        model.addAttribute("pageNum2", pageNum2);
        model.addAttribute("pageNum3", pageNum3);
        model.addAttribute("starTime", starTime);
        model.addAttribute("endTime",endTime);
        model.addAttribute("heightRiskArea",adminDataService.heightRiskArea().toString().replace("[","").replace("]",""));
        model.addAttribute("middleRiskArea",adminDataService.middleRiskArea().toString().replace("[","").replace("]",""));
        model.addAttribute("roles",userService.getUserNow().getRoles());
        model.addAttribute("num",adminDataService.selectAbnormal());
        return "risk_data";
    }

    @RequestMapping("/admin/dataexcel/{sort}/{starTime}/{endTime}/{body}/{istreatment}/{isolated}/{inschool}/{inguan}/{keyword}")
    public void dataExcel(HttpServletResponse response, @PathVariable String sort, @PathVariable String starTime, @PathVariable String endTime, @PathVariable String body
            , @PathVariable String istreatment, @PathVariable String isolated, @PathVariable String inschool, @PathVariable String inguan, @PathVariable String keyword) throws IOException {


        List<Daily_data> excellist = adminDataService.excelSelect(sort,starTime,endTime,body,istreatment,isolated,inschool,inguan,keyword);
        List<String> head = Arrays.asList("ID", "提交日期", "学号/工号", "手机号", "联系人", "联系人手机号", "身体状态", "是否就医", "是否隔离", "是否在校", "是否在莞", "目前所在地", "今日体温");
        excelUtil.writeExcel(response,excellist,head);
    }

    @RequestMapping("/admin/riskLocationExcel/{starTime}/{endTime}")
    public void riskLocationExcel(HttpServletResponse response, @PathVariable String starTime, @PathVariable String endTime) throws IOException {
        List<Daily_data> riskAreaData = adminDataService.riskAreaData(starTime,endTime);
        List<String> head = Arrays.asList("ID", "提交日期", "学号/工号", "手机号", "联系人", "联系人手机号", "身体状态", "是否就医", "是否隔离", "是否在校", "是否在莞", "目前所在地", "今日体温");
        excelUtil.writeExcel(response,riskAreaData,head);
    }

    @RequestMapping("/admin/riskTemperatureExcel/{starTime}/{endTime}")
    public void riskTemperatureExcel(HttpServletResponse response, @PathVariable String starTime, @PathVariable String endTime) throws IOException {
        List<Daily_data> riskTemperatureData = adminDataService.selectTemperature(starTime,endTime);
        List<String> head = Arrays.asList("ID", "提交日期", "学号/工号", "手机号", "联系人", "联系人手机号", "身体状态", "是否就医", "是否隔离", "是否在校", "是否在莞", "目前所在地", "今日体温");
        excelUtil.writeExcel(response,riskTemperatureData,head);
    }

    @RequestMapping("/admin/riskBodyExcel/{starTime}/{endTime}")
    public void riskBodyExcel(HttpServletResponse response, @PathVariable String starTime, @PathVariable String endTime) throws IOException {
        List<Daily_data> riskBodyData = adminDataService.excelSelect("",starTime,endTime,"!=","","","","","");
        List<String> head = Arrays.asList("ID", "提交日期", "学号/工号", "手机号", "联系人", "联系人手机号", "身体状态", "是否就医", "是否隔离", "是否在校", "是否在莞", "目前所在地", "今日体温");
        excelUtil.writeExcel(response,riskBodyData,head);
    }

}
